package com.hbm.usercenter.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hbm.usercenter.R;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by mjshe on 2017/6/28.
 */

public class GroupPayment extends LinearLayout {
    @Bind(R.id.iv_pay)
    ImageView mIvPay;
    @Bind(R.id.tv_pay)
    TextView mTvPay;
    @Bind(R.id.iv_isCheck)
    ImageView mIvIsCheck;
    @Bind(R.id.h_line)
    View mHLine;
    @Bind(R.id.ll)
    LinearLayout mLl;

    public GroupPayment(Context context) {
        super(context);
        initView(context);

    }

    public GroupPayment(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        initTypedArray(context, attrs);
    }

    public GroupPayment(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        initTypedArray(context, attrs);
    }

    private void initView(Context context) {
        if (!isInEditMode()) {
            View view = LayoutInflater.from(context).inflate(R.layout.group_payment, this, true);
            ButterKnife.bind(this, view);
        }
    }

    private void initTypedArray(Context context, AttributeSet attrs) {
        //获取在下面步骤4引用布局时在XML文件中定义的属性值
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.GroupPayment);

        //将获取到的值设置到相应位置
        if (null != typedArray) {
            Drawable icon = typedArray.getDrawable(R.styleable.GroupPayment_group_pay_icon);
            String text = typedArray.getString(R.styleable.GroupPayment_group_pay_text);
            //是否有分割线
            boolean isLine = typedArray.getBoolean(R.styleable.GroupPayment_group_pay_isLine, true);
            //是否选中
            boolean isCheck = typedArray.getBoolean(R.styleable.GroupPayment_group_pay_isCheck,
                    false);
            if (!isInEditMode()) {
                //设置TextView的文字
                if (!TextUtils.isEmpty(text)) {
                    mTvPay.setText(text);
                }
                //设置支付图标
                if (icon != null) {
                    mIvPay.setBackground(icon);
                }
                //是否显示下划线
                if (mHLine != null) {
                    if (isLine) {
                        mHLine.setVisibility(View.VISIBLE);
                    } else {
                        mHLine.setVisibility(View.INVISIBLE);
                    }
                }
                //                //输入类型是否为密码类型
                //                if (isPwd) {
                //                    mEtInput.setInputType(EditorInfo.TYPE_CLASS_TEXT | EditorInfo
                //                            .TYPE_TEXT_VARIATION_PASSWORD);
                //                } else {
                //
                //                }
            }
        }
        typedArray.recycle();
    }

    public void setIvIsCheck(boolean isCheck) {
        if (isCheck) {
            mIvIsCheck.setVisibility(VISIBLE);
        } else {
            mIvIsCheck.setVisibility(INVISIBLE);
        }

    }

    public void setClick(OnClickListener onclickListener) {
        mLl.setOnClickListener(onclickListener);
    }
}
